package baseserverframe;
import java.awt.*;
import javax.swing.*;



//------------------------------------------------------------------------------
import java.util.Vector;







/*******************************************************************************
 *
 * View class <br>
 *
 * It represents collection of components (typically one) and visualization data
 * from document. It also contains number of control components (hot bars)<br>
 * <br>
 * @author Truewar Ashman(<a href="mailto:truewar@hotmail.ru">truewar@hotmail.ru</a>) ID = 2009
 * @version 0.3<br>
 * @see document
 * @see mainFrame
 */

public class view
{
// +Variables for doc. ---------------------------------------------------------
	/** Name of document, which contains that view */
	private String	docName;
	/** Link to current document */
	private document docLink;
// -Variables for doc. ---------------------------------------------------------
// +Variables for view. --------------------------------------------------------
	/** Name of view */
	private String	viewName;
// +Variables for view. --------------------------------------------------------
// +Elements of view. ----------------------------------------------------------
	/** Menu for a view */
	private JMenuBar		menuBar;
	/** Collection of hot bars */
	private Vector <hotBar> HBContainer;
	/** Link to a view */
	private Component view;
// -Elements of view. ----------------------------------------------------------
	/**
	 *
	 * Internal class hotBar.
	 *
	 * Internal class, which describes hot bars.
	 *
	 * @author Truewar Ashman(<a href="mailto:truewar@hotmail.ru">truewar@hotmail.ru</a>)
	 * @version 0.3<br>
	 *
	 */
	private class hotBar
    {
		/** Logical name of hot bar. */
		private String		name;
		/** Link to component. */
        private Component	component;

		/**
		 * Class constructor.<br>
		 *
		 * @param name logical name.
		 * @param component link to component.
		 */
		public hotBar(String name, Component component)
		{
			this.name		= name;
			this.component	= component;
		}
    };
/**
 * Class constructor.<br>
 *
 */
	public view()
	{
		menuBar		= new JMenuBar();
		HBContainer = new Vector <hotBar> ();
		viewName = "";
	}

/**
 *  Sets view component.<br>
 *
 *  @param newView link to component.
 */
	public void setView(Component newView)
	{
		this.view = newView;
	}

/**
 *  Sets menu for view.<br>
 *
 *  @param Menu link to menu element.
 */
	public void setMenu(JMenuBar Menu)
	{
		this.menuBar = Menu;
	}

	/**
	 * Sets link to document, which has current view.
	 *
	 * @param dl - link to document.
	 */
	public void setDocLink(document dl)
	{
		this.docLink = dl;
	}
/**
 *  Removes menu.<br>
 */
	public void removeMenu()
	{
		this.menuBar = null;
	}

/**
 * Adds hot bar to view.
 *
 *	@param name logical name of hot bar.
 *  @param comp link to hot bar component.
 *  @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>true</b> - hot bar was successfully created.
 *			<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>false</b> - hot bar with that name was already registered.
 */
	public boolean addHB(String name, Component comp)
	{
		int cur = getHBIdByName(name);
		if(cur != -1) return false;

		hotBar newHB = new hotBar(name, comp);
		HBContainer.add(newHB);
		return true;
	}
/**
 *  Removes hot bar from view by name.<br>
 *
 *	@param name logical name of hot bar.
 *  @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>true</b> - hot bar was successfully removed.
 *			<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>false</b> - there are ni hot bar with such name.
 */
	public boolean removeHB(String name)
	{
		int cur = getHBIdByName(name);
		if(cur == -1) return false;

		HBContainer.remove(cur);
		return true;
	}
/**
 *  Removes hot bar from view by index.<br>
 *
 *	@param id index of hot bar.
 *  @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>true</b> - hot bar was successfully removed.
 *			<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>false</b> - there are ni hot bar with such index.
 */
	public boolean removeHB(int id)
	{
		if((id < 0)||(id > HBContainer.size()))
			return false;
		HBContainer.remove(id);
		return true;
	}

/**
 *  Redefinise name of document and view.<br>
 *
 *	@param docName logical name of document, which contains that view.
 *	@param viewName logical name of view.
 */
	public void reregister(String docName, String viewName)
	{
		this.docName = docName;
		this.viewName = viewName;
	}
// +getSomething----------------------------------------------------------------
/**
 *  Returns name of view.<br>
 *
 *  @return name of view.
 */
	public String getViewName()
	{
		return viewName;
	}
/**
 *  Returns link to menu.<br>
 *
 *  @return link to menu.
 */
	public JMenuBar getMenu()
	{
		return menuBar;
	}
/**
 *  Returns link to hot bar by index.<br>
 *
 *  @return link to hot bar or null.
 */
	public Component getHotBar(int id)
	{
		if((id < 0)||(id > HBContainer.size()))
			return null;
		return HBContainer.get(id).component;
	}
/**
 *  Returns link to view.
 *
 *  @return link to view.
 */
	public Component getView()
	{
		return view;
	}
/**
 *  Returns number of hot bars.<br>
 *
 *  @return number of hot bars.
 */
	public int getNumOfHB()
	{
		return HBContainer.size();
	}

	/**
	 * 
	 * @return link to document, which has current view.
	 */
	public document getDocLink()
	{
		return docLink;
	}
// -getSomething----------------------------------------------------------------
// +private---------------------------------------------------------------------
/**
 * Method to get index of hot bar by name.<br>
 *
 * @param name logical name of hot bar.
 * @return <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>int</b> - index of hot bar.
 */
	private int getHBIdByName(String name)
	{
		for(int i = 0; i < HBContainer.size(); i++)
		{
			if(HBContainer.get(i).name.compareTo(name) == 0)
				return i;
		}
		return -1;
	}
// -private---------------------------------------------------------------------
}
